package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.FamilyDao;
import cn.sumpu.app.guoguojie.domain.Comment;
import cn.sumpu.app.guoguojie.domain.Family;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class FamilyDaoImpl extends SqlSessionDaoSupport implements FamilyDao {

	private static Logger logger = LoggerFactory.getLogger(FamilyDaoImpl.class);

	public void insert(Family family) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.FamilyMapper.insert",
					family);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<Family> getFamilyList(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getFamilyList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	@Override
	public void updateFamily(Family family) throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.FamilyMapper.updateFamily",
							family);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update family failure.", e);
			}
			throw new AppBusinessException("update family failure.", e);
		}
	}

	// 通过会员编号查询得到家的信息
	public Family getFamilyByMember(String member) throws AppBusinessException {
		try {
			return (Family) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getFamilyByMember",
							member);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getFamilyByMember database failure.", e);
			}
			throw new AppBusinessException(
					"getFamilyByMember database failure.", e);
		}
	}

	// 更改家庭积分
	@Override
	public void updateFamilyIntegral(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.FamilyMapper.updateFamilyIntegral",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateFamilyIntegral family failure.", e);
			}
			throw new AppBusinessException(
					"updateFamilyIntegral family failure.", e);
		}
	}

	// 根据连续登录天数加分
	@Override
	public void updateFamilyByLogin(Map<String, Object> map)
			throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.FamilyMapper.updateFamilyByLogin",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("updateFamilyByLogin family failure.", e);
			}
			throw new AppBusinessException(
					"updateFamilyByLogin family failure.", e);
		}
	}

	// 根据门牌号取得这家最近的两条微博(不是同一天发的)
	@SuppressWarnings("unchecked")
	@Override
	public List<String> getDoingTwoDay(String doorplate)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getDoingTwoDay",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoingTwoDay failure.", e);
			}
			throw new AppBusinessException("getDoingTwoDay failure.", e);
		}
	}

	// 根据门牌号取得这家最近的两篇日志(不是同一天发的)
	@SuppressWarnings("unchecked")
	@Override
	public List<String> getBlogTwoDay(String doorplate)
			throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getBlogTwoDay",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBlogTwoDay failure.", e);
			}
			throw new AppBusinessException("getBlogTwoDay failure.", e);
		}
	}

	// 根据门牌号取得这家今天发表了多少条微博（包括对别人微博的评论）
	@Override
	public int getDoingCommentCount(String doorplate)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getDoingCommentCount",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoingCommentCount failure.", e);
			}
			throw new AppBusinessException("getDoingCommentCount failure.", e);
		}
	}

	// 根据门牌号取得这家今天发表了多少篇日志（包括对别人日志的评论）
	@Override
	public int getBlogCommentCount(String doorplate)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getBlogCommentCount",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBlogCommentCount failure.", e);
			}
			throw new AppBusinessException("getBlogCommentCount failure.", e);
		}
	}

	// 根据门牌号取得今天这家添加了几个邻居
	@Override
	public int getAddFriendCount(String doorplate) throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getAddFriendCount",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAddFriendCount failure.", e);
			}
			throw new AppBusinessException("getAddFriendCount failure.", e);
		}
	}

	// 根据门牌号取得今天这家同意了几个邻居的邀请
	@Override
	public int getAgreeFriendCount(String doorplate)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getAgreeFriendCount",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getAgreeFriendCount failure.", e);
			}
			throw new AppBusinessException("getAgreeFriendCount failure.", e);
		}
	}

	// 根据门牌号取得这家今天微博的最高评论量
	@Override
	public Comment getDoingTopCommentCount(String doorplate)
			throws AppBusinessException {
		try {
			return (Comment) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getDoingTopCommentCount",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getDoingTopCommentCount failure.", e);
			}
			throw new AppBusinessException("getDoingTopCommentCount failure.",
					e);
		}
	}

	// 根据门牌号取得这家今天日志的最高评论量
	@Override
	public Comment getBlogTopCommentCount(String doorplate)
			throws AppBusinessException {
		try {
			return (Comment) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getBlogTopCommentCount",
							doorplate);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getBlogTopCommentCount failure.", e);
			}
			throw new AppBusinessException("getBlogTopCommentCount failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Family> getLoveFamilyList(Map<String, Object> map) throws AppBusinessException {		
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getLoveFamilyList",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getLoveFamilyList failure.", e);
			}
			throw new AppBusinessException("getLoveFamilyList failure.", e);
		}

	}

	@Override
	public int getLoveFamilyListCount(Map<String, Object> map)
			throws AppBusinessException {
		try {
			return (Integer) getSqlSession()
					.selectOne(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getLoveFamilyListCount",map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getLoveFamilyListCount failure.", e);
			}
			throw new AppBusinessException("getLoveFamilyListCount failure.", e);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<String> getAllFamilyId() throws AppBusinessException {
		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.FamilyMapper.getAllFamilyId");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

}
